<?php
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module einstein

// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework

/**
 *	@package dotProject
 *	@subpackage modules
 *	@version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once( $AppUI->getSystemClass ('dp' ) );		// use the dPFramework for easy inclusion of this class here
require_once( $AppUI->getMasterDataClass ('mdunit' ) );		// use the dPFramework for easy inclusion of this class here
require_once( $AppUI->getMasterDataClass ('mdtype' ) );		// use the dPFramework for easy inclusion of this class here

/**
 * The Item Class
 */
class CItem extends CDpObject {
	// link variables to the einstein object (according to the existing columns in the database table einstein)
	var $item_id = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $item_code = NULL;
	var $item_name = NULL;
	var $item_status = NULL;
	var $item_type1 = NULL;
	var $item_type2 = NULL;
	var $item_type3 = NULL;
	var $item_type4 = NULL;
	var $item_type5 = NULL;
	var $item_quantity_unit = NULL;
	var $item_volume_unit = NULL;
	var $item_weight_unit = NULL;
	var $item_division = NULL;
	var $item_costcenter = NULL;
	var $item_resource = NULL;
	var $item_supplier = NULL;
	var $item_customer = NULL;
	var $item_ledger1 = NULL;
	var $item_ledger2 = NULL;
	var $item_vat_code = NULL;
	var $item_be_check = 0;

	//ALTER TABLE  `items` ADD  `item_be_check` TINYINT( 3 ) NOT NULL DEFAULT  '0';


	// the constructor of the CEinstein class, always combined with the table name and the unique key of the table
	function CItem() {
		$joins = array(
			array( 'label' => 'Reference line', 'name' => 'project_items', 'idfield' => 'pitem_id', 'joinfield' => 'pitem_item_id' ),
			array( 'label' => 'Quotation line', 'name' => 'quotation_lines', 'idfield' => 'qitem_id', 'joinfield' => 'qitem_item' ),
			array( 'label' => 'Tasks', 'name' => 'tasks', 'idfield' => 'task_id', 'joinfield' => 'task_item' ),
			array( 'label' => 'Task log', 'name' => 'task_log', 'idfield' => 'task_log_id', 'joinfield' => 'task_log_item' ),
		);
		$this->CDpObject( 'items', 'item_id', 'item_code', $joins, 'items' );
	}

	function check() {
		if (!isset($this->_tbl_unique) || $this->_tbl_unique == NULL) {
			return NULL;
		}
		$q = new DBQuery();
		$q->addTable("$this->_tbl");
		$q->addWhere("$this->_tbl_key != $this->item_id");
		$q->addWhere("$this->_tbl_unique = '$this->item_code'");
		$result = $q->loadResult();
		if (isset($result) && $result != '') {
			return 'Duplicate item code: ' . $this->item_code;
		}
		return NULL;
	}

 	//overide method load
 	function load($id) {
 		$result = parent::load($id);
 		if (!$result) {
 			return false;
 		}
 		$t = new CMdType();
 		$t->load($this->item_type1);
 		$this->item_type1_name = $t->name;
 		return true;
 	}

 	//overide method load
 	function loadByCode($code) {
 		$q = new DBQuery();
 		$q->addTable('items');
 		$q->addWhere("item_code = '$code'");
 		$sql = $q->prepare();
 		$i = db_loadList($sql);
 		parent::load($i[0]['item_id']);

 		$t = new CMdUnit();

 		$t->load($this->item_quantity_unit);
 		$this->qunit_name = @$t->unit_name;

 		$t->load($this->item_weight_unit);
 		$this->wunit_name = @$t->unit_name;

 		$t->load($this->item_volume_unit);
 		$this->vunit_name = @$t->unit_name;

 		$t = new CMdType();
 		$t->load($this->item_type1);
 		$this->item_type1_name = $t->name;
 	}


    /**
     *
     *
     */
    function getItemsList(){
        $q = new DBQuery;
        $q->addTable('items');
        $q->addQuery('items.*, t.name as item_type_name, s.status_name as item_status_name');
        $q->addJoin('md_type', 't', 't.type_id = item_type1');
        $q->addJoin('md_status', 's', 's.status_id = item_status');
        $q->addOrder('item_type1, item_name, item_status');
        $sql = $q->prepare();
        $q->clear();
        return db_loadList($sql);

    }

    function getItemsList4AutoCompleter($screen = null){
		$q  = new DBQuery;

		$q = new DBQuery;
	    $q->addTable('items');
	    $q->addQuery('items.item_id as id, items.item_code as code, items.item_name as name, items.item_vat_code as vat_code, t.name as type');
		$q->addJoin('md_type',  "t", "type_id = item_type1");
		if (isset($screen)) {
			$q->addJoin('item_screens', 'scr', 'scr.item_type = t.type_id');
			$q->addWhere("item_screen='" . $screen . "'");
		}

	    $q->addOrder('item_type1, items.item_name');
	    $sql = $q->prepare();
	    $q->clear();

	    // Store varaible temper
	    $tmpList = db_loadList($sql);
	    $itemList = array();
	    $tmp = null;
	    $vatCodeArr = dPgetSysVal('ItemVATCode');
		foreach($tmpList as $key => $val){
			$tmp = new stdClass();
			$tmp->id = $val['id'];
			$tmp->code = $val['code'];
			$tmp->name = $val['name'];
			$tmp->vat_code_value = $val['vat_code'];
			$tmp->vat_code = $vatCodeArr[$val['vat_code']];
			$tmp->type = $val['type'];
			$itemList[] = $tmp;
		}
		return $itemList;
	}
}
?>